package com.Jano.listener;

import com.Jano.service.CategoryService;
import com.Jano.service.Impl.CategoryServiceImpl;
import com.Jano.timer.UpdateTimer;

import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

/**
 * description: Mylistener <br>
 * date: 2022/6/9 7:59 PM <br>
 * author: zoumaoji <br>
 * version: 1.0 <br>
 */
@WebListener
public class MyServletContextListener implements ServletContextListener, ServletContextAttributeListener {
    @Override
    public void contextInitialized(ServletContextEvent event) {
        //服务器启动的时候
        System.out.println("应用被初始化了...");
        System.out.println("启动定时器");
        System.out.println("从数据库读取商品目录信息存储到Redis");
        //定时更新
        UpdateTimer updateTimer = new UpdateTimer();
        updateTimer.productUpdateTimer();
        //将目录信息存储到Redis
        CategoryService categoryService = new CategoryServiceImpl();
        categoryService.saveAllCategoriesInRedis();
    }

    @Override
    public void contextDestroyed(ServletContextEvent event) {
        //服务器关闭的时候
        System.out.println("应用被销毁了...");
    }

    @Override
    public void attributeAdded(ServletContextAttributeEvent event) {
        System.out.println("在ServletContext中添加了一个新的属性");
        System.out.println("name:" + event.getName() + ",value:" + event.getValue());
        System.out.println("==============================");
    }

    @Override
    public void attributeRemoved(ServletContextAttributeEvent event) {
        System.out.println("在ServletContext中移除了的属性");
        System.out.println("name:" + event.getName() + ",value:" + event.getValue());
        System.out.println("==============================");
    }

    @Override
    public void attributeReplaced(ServletContextAttributeEvent event) {
        System.out.println("在ServletContext中替换了属性");
        System.out.println("name:" + event.getName() + ",旧的value:" + event.getValue());
        System.out.println("==============================");
    }
}
